package com.resourcestore.action;

import java.io.UnsupportedEncodingException;

import java.text.ParseException;
import java.util.List;

import com.resourcestore.utils.CommonUtil;
import com.resourcestore.utils.tag.Pagination;
import com.resourcestore.action.BaseAction;
import com.resourcestore.dao.ClassStuDao;
import com.resourcestore.dao.MajorGroupDao;
import com.resourcestore.dao.MajorDao;
import com.resourcestore.model.ClassStu;
import com.resourcestore.model.MajorGroup;
import com.resourcestore.model.Major;

public class ClassStuAction extends BaseAction {

	private ClassStu classStu;
	private List<ClassStu> listClassStu;
	private ClassStuDao classStuDao = new ClassStuDao();
	private Pagination pagination = new Pagination();
	private MajorGroupDao majorGroupDao = new MajorGroupDao();
	private MajorDao majorDao = new MajorDao();
	private List<MajorGroup> listMajorGroupOption;
	private List<Major> listMajorOption;

	/**
	 * 查询返回List带分页
	 * 
	 * @throws ParseException
	 * 
	 */

	public String listClassStu() {
		classStu = new ClassStu();
		pagination.setTotal(classStuDao.queryClassStuNum(classStu));
		listClassStu = classStuDao.selectClassStuPage(classStu, pagination);

		// 将bigint数字处理为时间格式
		for (int i = 0; i < listClassStu.size(); i++) {
			try {
				listClassStu.get(i).setGmtCreatedString(
						CommonUtil.getSysTimeFormatString(listClassStu.get(i)
								.getGmtCreated()));
				listClassStu.get(i).setGmtModifiedString(
						CommonUtil.getSysTimeFormatString(listClassStu.get(i)
								.getGmtModified()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return "default";
	}

	/**
	 * 根据条件搜索，查询返回List带分页
	 * 
	 * @throws ParseException
	 * 
	 */
	public String searchListClassStu() {

		// 对内容进行urldecoder解码
		try {
			classStu.setClassName(new String(classStu.getClassName().getBytes(
					"iso-8859-1"), "UTF-8"));
			classStu.setClassRemark(new String(classStu.getClassRemark()
					.getBytes("iso-8859-1"), "UTF-8"));
			classStu.setMajorName(new String(classStu.getMajorName().getBytes(
					"iso-8859-1"), "UTF-8"));
			classStu.setDepartmentName(new String(classStu.getDepartmentName()
					.getBytes("iso-8859-1"), "UTF-8"));
			classStu.setAuthor(new String(classStu.getAuthor().getBytes(
					"iso-8859-1"), "UTF-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		pagination.setTotal(classStuDao.queryClassStuNum(classStu));
		listClassStu = classStuDao.selectClassStuPage(classStu, pagination);

		// 将bigint数字处理为时间格式
		for (int i = 0; i < listClassStu.size(); i++) {
			try {
				listClassStu.get(i).setGmtCreatedString(
						CommonUtil.getSysTimeFormatString(listClassStu.get(i)
								.getGmtCreated()));
				listClassStu.get(i).setGmtModifiedString(
						CommonUtil.getSysTimeFormatString(listClassStu.get(i)
								.getGmtModified()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return newpath("/ClassStu/listClassStu.jsp");
	}

	/**
	 * 预处理插入数据
	 */
	public String preInsertClassStu() {

		listMajorGroupOption = majorGroupDao.selectMajorGroup();

		listMajorOption = majorDao.selectMajor();

		return newpath("/ClassStu/insertClassStu.jsp");
	}

	/**
	 * 插入一条数据
	 */
	public String insertClassStu() {

		try {
			classStu.setGmtCreated(CommonUtil.getSysTimeFormatNum());
			classStu.setGmtModified(CommonUtil.getSysTimeFormatNum());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		classStuDao.insertClassStu(classStu);
		return redirect(this.getUrl());
	}

	/**
	 * 更新之前加载原信息
	 * 
	 */
	public String preUpdateClassStu() {
		classStu = classStuDao.selectClassStuById(classStu);

		try {
			classStu.setGmtCreatedString(CommonUtil
					.getSysTimeFormatString(classStu.getGmtCreated()));
			classStu.setGmtModifiedString(CommonUtil
					.getSysTimeFormatString(classStu.getGmtModified()));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return newpath("/ClassStu/updateClassStu.jsp");
	}

	/**
	 * 更新信息
	 * 
	 */
	public String updateClassStu() {

		try {
			// classStu.setGmtCreated(CommonUtil.getSysTimeFormatNum());
			classStu.setGmtModified(CommonUtil.getSysTimeFormatNum());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		classStuDao.updateClassStu(classStu);
		return redirect(this.getUrl());
	}

	/**
	 * 删除信息
	 */
	public String deleteClassStu() {
		classStuDao.deleteClassStu(classStu);
		return redirect(this.getUrl());
	}

	/**
	 * 批量删除信息
	 */
	public String deleteMoreClassStu() {

		classStuDao.deleteMoreClassStu(classStu);
		return redirect(this.getUrl());
	}

	public ClassStu getClassStu() {
		return classStu;
	}

	public void setClassStu(ClassStu classStu) {
		this.classStu = classStu;
	}

	public List<ClassStu> getListClassStu() {
		return listClassStu;
	}

	public void setListClassStu(List<ClassStu> listClassStu) {
		this.listClassStu = listClassStu;
	}

	public Pagination getPagination() {
		return this.pagination;
	}

	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}

	public List<MajorGroup> getListMajorGroupOption() {
		return listMajorGroupOption;
	}

	public void setListMajorGroupOption(List<MajorGroup> listMajorGroupOption) {
		this.listMajorGroupOption = listMajorGroupOption;
	}

	public List<Major> getListMajorOption() {
		return listMajorOption;
	}

	public void setListMajorOption(List<Major> listMajorOption) {
		this.listMajorOption = listMajorOption;
	}

}
